/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ftpcombiner.server.virtualfiles;

import ftpcombiner.server.serverconnection.Folder;
import java.util.Enumeration;
import javax.swing.tree.TreeNode;

/**
 *
 * @author Gary
 */
public class VirtualFolder extends VirtualItem<Folder> {

    private VirtualItem[] children;

    public VirtualFolder(Folder alias, VirtualFolder parent) {
        super(alias, parent);
    }

    public VirtualItem[] getChildren() {
        return children;
    }

    public boolean isRoot() {
        return alias == null && parent == null;
    }

    /**
     * Gets the child item matching the given description.
     *
     * @param description
     * @return The child matching the given description.
     * @throws IllegalArgumentException If the child is not found.
     */
    public VirtualItem getChild(String description) {
        int i = 0;
        for (VirtualItem child : children) {
            if (child.getName().equals(description)) {
                child.index = i;
                return child;
            }
            i++;
        }
        throw new IllegalArgumentException("The child matching \"" + description + "\" was not found.");
    }

    /**
     * Gets the child at the given index.
     *
     * @param index
     * @return The child at the given index.
     * @throws ArrayIndexOutOfBoundsException
     */
    @Override
    public VirtualItem getChildAt(int index) {
        VirtualItem item = null;
        if (children != null) {
            item = children[index];
            item.index = index;
        }
        return item;
    }

    public void setChildren(VirtualItem[] children) {
        this.children = children;
    }

    @Override
    public int getChildCount() {
        return children.length;
    }

    @Override
    public int getIndex(TreeNode node) {
        return getChild(((VirtualItem) node).getName()).getIndex();
    }

    @Override
    public boolean getAllowsChildren() {
        return true;
    }

    @Override
    public boolean isLeaf() {
        //System.out.println("isLeaf");
        return false;
    }

    @Override
    public Enumeration children() {
        return new ArrayEnumeration(children);
    }
}
